﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Helix_Network_Supervisor_3
{
    public partial class ChannelMapForm : Form
    {
        #region Fields

        const int maxRenard = 286;
        private int helixAddress;

        #endregion Fields

        #region Ctor

        public ChannelMapForm()
        {
            InitializeComponent();
        }

        #endregion Ctor

        #region Public Methods

        public void updateHelixSystemLabel(int helixCnt)
        {
            helixSystemLabel.Text = "Helix System " + helixCnt.ToString();
            helixAddress = helixCnt;
        } //End updateHelixSystemLabel

        #endregion Public Methods

        #region Private Methods

        private void chB1FunctionComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //If the function is not Unused then need to enable start and end channel selectors
            if (chB1FunctionComboBox.SelectedIndex != 0)
            {
                chB1StartChNumUpDown.Enabled = true;
                chB1EndChNumUpDown.Enabled = true;
            }
            else
            {
                chB1StartChNumUpDown.Enabled = false;
                chB1EndChNumUpDown.Enabled = false;
            }
        } //End chB1FunctionComboBox_SelectedIndexChanged

        private void chB2FunctionComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (chB2FunctionComboBox.SelectedIndex != 0)
            {
                chB2StartChNumUpDown.Enabled = true;
                chB2EndChNumUpDown.Enabled = true;
            }
            else
            {
                chB2StartChNumUpDown.Enabled = false;
                chB2EndChNumUpDown.Enabled = false;
            }
        } //chB2FunctionComboBox_SelectedIndexChanged

        private void chB3FunctionComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (chB3FunctionComboBox.SelectedIndex != 0)
            {
                chB3StartChNumUpDown.Enabled = true;
                chB3EndChNumUpDown.Enabled = true;
            }
            else
            {
                chB3StartChNumUpDown.Enabled = false;
                chB3EndChNumUpDown.Enabled = false;
            }
        } //chB3FunctionComboBox_SelectedIndexChanged

        private void chB4FunctionComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (chB4FunctionComboBox.SelectedIndex != 0)
            {
                chB4StartChNumUpDown.Enabled = true;
                chB4EndChNumUpDown.Enabled = true;
            }
            else
            {
                chB4StartChNumUpDown.Enabled = false;
                chB4EndChNumUpDown.Enabled = false;
            }
        } //chB4FunctionComboBox_SelectedIndexChanged

        #endregion Private Methods

        #region Properties

        public string chB1Function
        {
            get
            {
                switch (chB1FunctionComboBox.SelectedIndex)
                {
                    case 0:
                        return "Unused";

                    case 1:
                        return "Helix";

                    default:
                        return "Unused";
                } // End switch
            } // End get
            set
            {
                switch (value)
                {
                    case "Unused":
                        chB1FunctionComboBox.SelectedIndex = 0;
                        break;

                    case "Helix":
                        chB1FunctionComboBox.SelectedIndex = 1;
                        break;

                    default:
                        chB1FunctionComboBox.SelectedIndex = 0;
                        break;
                } // End switch
            } // End set
        } // End chB1Function

        public string chB2Function
        {
            get
            {
                switch (chB2FunctionComboBox.SelectedIndex)
                {
                    case 0:
                        return "Unused";

                    case 1:
                        return "Helix";

                    case 2:
                        return "Renard";

                    case 3:
                        return "DMX";

                    default:
                        return "Unused";
                } // End switch
            } // End get
            set
            {
                switch (value)
                {
                    case "Unused":
                        chB2FunctionComboBox.SelectedIndex = 0;
                        break;

                    case "Helix":
                        chB2FunctionComboBox.SelectedIndex = 1;
                        break;

                    case "Renard":
                        chB2FunctionComboBox.SelectedIndex = 2;
                        break;

                    case "DMX":
                        chB2FunctionComboBox.SelectedIndex = 3;
                        break;

                    default:
                        chB2FunctionComboBox.SelectedIndex = 0;
                        break;
                } // End switch
            } // End set
        } // End chB2Function

        public string chB3Function
        {
            get
            {
                switch (chB3FunctionComboBox.SelectedIndex)
                {
                    case 0:
                        return "Unused";

                    case 1:
                        return "Helix";

                    case 2:
                        return "Renard";

                    case 3:
                        return "DMX";

                    default:
                        return "Unused";
                } // End switch
            } // End get
            set
            {
                switch (value)
                {
                    case "Unused":
                        chB3FunctionComboBox.SelectedIndex = 0;
                        break;

                    case "Helix":
                        chB3FunctionComboBox.SelectedIndex = 1;
                        break;

                    case "Renard":
                        chB3FunctionComboBox.SelectedIndex = 2;
                        break;

                    case "DMX":
                        chB3FunctionComboBox.SelectedIndex = 3;
                        break;

                    default:
                        chB3FunctionComboBox.SelectedIndex = 0;
                        break;
                } // End switch
            } // End set
        } // End chB3Function

        public string chB4Function
        {
            get
            {
                switch (chB4FunctionComboBox.SelectedIndex)
                {
                    case 0:
                        return "Unused";

                    case 1:
                        return "Helix";

                    case 2:
                        return "Renard";

                    case 3:
                        return "DMX";

                    default:
                        return "Unused";
                } // End switch
            } // End get
            set
            {
                switch (value)
                {
                    case "Unused":
                        chB4FunctionComboBox.SelectedIndex = 0;
                        break;

                    case "Helix":
                        chB4FunctionComboBox.SelectedIndex = 1;
                        break;

                    case "Renard":
                        chB4FunctionComboBox.SelectedIndex = 2;
                        break;

                    case "DMX":
                        chB4FunctionComboBox.SelectedIndex = 3;
                        break;

                    default:
                        chB4FunctionComboBox.SelectedIndex = 0;
                        break;
                } // End switch
            } // End set
        } // End chB4Function

        public int chB1StartChannel
        {
            get
            {
                return (int)chB1StartChNumUpDown.Value;
            }
            set
            {
                chB1StartChNumUpDown.Value = (decimal)value;
            }
        } // End chB1StartChannel

        public int chB1EndChannel
        {
            get
            {
                return (int)chB1EndChNumUpDown.Value;
            }
            set
            {
                chB1EndChNumUpDown.Value = (decimal)value;
            }
        } // End chB1EndChannel

        public int chB2StartChannel
        {
            get
            {
                return (int)chB2StartChNumUpDown.Value;
            }
            set
            {
                chB2StartChNumUpDown.Value = (decimal)value;
            }
        } // End chB2StartChannel

        public int chB2EndChannel
        {
            get
            {
                return (int)chB2EndChNumUpDown.Value;
            }
            set
            {
                chB2EndChNumUpDown.Value = (decimal)value;
            }
        } // End chB2EndChannel

        public int chB3StartChannel
        {
            get
            {
                return (int)chB3StartChNumUpDown.Value;
            }
            set
            {
                chB3StartChNumUpDown.Value = (decimal)value;
            }
        } // End chB3StartChannel

        public int chB3EndChannel
        {
            get
            {
                return (int)chB3EndChNumUpDown.Value;
            }
            set
            {
                chB3EndChNumUpDown.Value = (decimal)value;
            }
        } // End chB3EndChannel

        public int chB4StartChannel
        {
            get
            {
                return (int)chB4StartChNumUpDown.Value;
            }
            set
            {
                chB4StartChNumUpDown.Value = (decimal)value;
            }
        } // End chB4StartChannel

        public int chB4EndChannel
        {
            get
            {
                return (int)chB4EndChNumUpDown.Value;
            }
            set
            {
                chB4EndChNumUpDown.Value = (decimal)value;
            }
        }

        #endregion Properties

        #region Events

        private void ChannelMapForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult == DialogResult.OK)
            {
                bool validated = true;
                int channelCnt;

                // Validate Channel Bank 1
                channelCnt = chB1EndChannel - chB1StartChannel + 1;
                if ((channelCnt <= 0) & (chB1Function != "Unused")) // Check for End Channel less than Start Channel
                {
                    MessageBox.Show("Channel Bank 1's end channel is less than its start channel.  " +
                        "Please update the start and/or end channels",
                        "Channel Bank 1 Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    validated = false;
                }
                switch (chB1Function)
                {
                    case "Helix":
                        if (channelCnt > 32)
                        {
                            MessageBox.Show("Channel Bank 1 exceeds the maximum of 32 channels for the Helix function.  " +
                            "Please update the start and/or end channels",
                                "Channel Bank 1 Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            validated = false;
                        }
                        break;
                } // End ChB1 Switch

                // Validate Channel Bank 2
                channelCnt = chB2EndChannel - chB2StartChannel + 1;
                if ((channelCnt <= 0) & (chB2Function != "Unused")) // Check for End Channel less than Start Channel
                {
                    MessageBox.Show("Channel Bank 2's end channel is less than its start channel.  " +
                        "Please update the start and/or end channels",
                        "Channel Bank 2 Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    validated = false;
                }

                switch (chB2Function)
                {
                    case "Helix":
                        if (channelCnt > 32)
                        {
                            MessageBox.Show("Channel Bank 2 exceeds the maximum of 32 channels for the Helix function.  " +
                                "Please update the start and/or end channels",
                                "Channel Bank 2 Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            validated = false;
                        }
                        break;

                    case "Renard":
                        if (channelCnt > maxRenard)
                        {
                            MessageBox.Show("Channel Bank 2 exceeds the maximum of" + maxRenard +
                            " channels for the Renard function.  Please update the start and/or end channels",
                                "Channel Bank 2 Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            validated = false;
                        }
                        break;

                    case "DMX":
                        if (channelCnt > 512)
                        {
                            MessageBox.Show("Channel Bank 2 exceeds the maximum of 512 channels for the DMX function.  " +
                                "Please update the start and/or end channels",
                                "Channel Bank 2 Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            validated = false;
                        }
                        break;
                } // End ChB2 Switch

                // Validate Channel Bank 3
                channelCnt = chB3EndChannel - chB3StartChannel + 1;
                if ((channelCnt <= 0) & (chB3Function != "Unused")) // Check for End Channel less than Start Channel
                {
                    MessageBox.Show("Channel Bank 3's end channel is less than its start channel.  " +
                        "Please update the start and/or end channels",
                        "Channel Bank 3 Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    validated = false;
                }

                switch (chB3Function)
                {
                    case "Helix":
                        if (channelCnt > 32)
                        {
                            MessageBox.Show("Channel Bank 3 exceeds the maximum of 32 channels for the Helix function.  " +
                                "Please update the start and/or end channels",
                                "Channel Bank 3 Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            validated = false;
                        }
                        break;

                    case "Renard":
                        if (channelCnt > maxRenard)
                        {
                            MessageBox.Show("Channel Bank 3 exceeds the maximum of" + maxRenard +
                            " channels for the Renard function.  Please update the start and/or end channels",
                                "Channel Bank 3 Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            validated = false;
                        }
                        break;

                    case "DMX":
                        if (channelCnt > 512)
                        {
                            MessageBox.Show("Channel Bank 3 exceeds the maximum of 512 channels for the DMX function.  " +
                                "Please update the start and/or end channels",
                                "Channel Bank 3 Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            validated = false;
                        }
                        break;
                } // End ChB3 Switch

                // Validate Channel Bank 4
                channelCnt = chB4EndChannel - chB4StartChannel + 1;
                if ((channelCnt <= 0) & (chB4Function != "Unused")) // Check for End Channel less than Start Channel
                {
                    MessageBox.Show("Channel Bank 4's end channel is less than its start channel.  " +
                        "Please update the start and/or end channels",
                        "Channel Bank 4 Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    validated = false;
                }

                switch (chB4Function)
                {
                    case "Helix":
                        if (channelCnt > 32)
                        {
                            MessageBox.Show("Channel Bank 4 exceeds the maximum of 32 channels for the Helix function.  " +
                                "Please update the start and/or end channels",
                                "Channel Bank 4 Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            validated = false;
                        }
                        break;

                    case "Renard":
                        if (channelCnt > maxRenard)
                        {
                            MessageBox.Show("Channel Bank 4 exceeds the maximum of" + maxRenard +
                            " channels for the Renard function.  Please update the start and/or end channels",
                                "Channel Bank 4 Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            validated = false;
                        }
                        break;

                    case "DMX":
                        if (channelCnt > 512)
                        {
                            MessageBox.Show("Channel Bank 4 exceeds the maximum of 512 channels for the DMX function.  " +
                                "Please update the start and/or end channels",
                                "Channel Bank 4 Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            validated = false;
                        }
                        break;
                } // End ChB4 Switch

                if (!validated)
                {
                    e.Cancel = true;
                }
            } // End If (DialogResult...)
        }

        private void standardHelixButton_Click(object sender, EventArgs e)
        {
            chB1FunctionComboBox.SelectedIndex = 1;
            decimal offset = (decimal)((helixAddress - 1) * 128);
            chB1StartChNumUpDown.Value = 1 + offset;
            chB1EndChNumUpDown.Value = 32 + offset;
            chB2FunctionComboBox.SelectedIndex = 1;
            chB2StartChNumUpDown.Value = 33 + offset;
            chB2EndChNumUpDown.Value = 64 + offset;
            chB3FunctionComboBox.SelectedIndex = 1;
            chB3StartChNumUpDown.Value = 65 + offset;
            chB3EndChNumUpDown.Value = 96 + offset;
            chB4FunctionComboBox.SelectedIndex = 1;
            chB4StartChNumUpDown.Value = 97 + offset;
            chB4EndChNumUpDown.Value = 128 + offset;
        }

        #endregion Events
    }
}
